package com.weixingtu.commuterCar.servlet;

import com.weixingtu.commuterCar.bean.Message;
import com.weixingtu.commuterCar.util.JSONUtil;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.PMML;
import org.jpmml.evaluator.*;
import org.jpmml.model.PMMLUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/getPredictedTimeServlet.do")
public class getPredictedTimeServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        Map<String, Double> map = new HashMap<>();
        double device_id = Double.parseDouble(request.getParameter("device_id"));
        double predicted_stop_id = Double.parseDouble(request.getParameter("predicted_stop_id"));
        double longitude = Double.parseDouble(request.getParameter("longitude"));
        double latitude = Double.parseDouble(request.getParameter("latitude"));
        double velocity = Double.parseDouble(request.getParameter("velocity"));
        double weather_temperature = Double.parseDouble(request.getParameter("weather_temperature"));
        double weather_info = 10.0;
        String weather = request.getParameter("weather_temperature");
        if(weather == "晴")
            weather_info = 0.0;
        else if(weather == "多云")
            weather_info = 1.0;
        else if(weather == "阴")
            weather_info = 2.0;
        else if(weather == "阵雨" || weather == "雷阵雨")
            weather_info = 3.0;
        else if(weather == "雨夹雪" || weather == "阵雪" || weather == "小雪" || weather == "中雪" || weather == "大雪" || weather == "暴雪" || weather == "小到中雪" || weather == "中到大雪" || weather == "大到暴雪")
            weather_info = 4.0;
        else if(weather == "小雨" || weather == "小到中雨")
            weather_info = 5.0;
        else if(weather == "中雨" || weather == "中到大雨")
            weather_info = 6.0;
        else if(weather == "大雨" || weather == "大到暴雨")
            weather_info = 7.0;
        else if(weather == "暴雨" || weather == "大暴雨" || weather == "特大暴雨" || weather == "暴雨到大暴雨" || weather == "大暴雨到特大暴雨")
            weather_info = 8.0;
        //雾 浮沉 扬沙 霾
        else if(weather == "雾" || weather == "浮沉" || weather == "扬沙" || weather == "霾")
            weather_info = 9.0;

        map.put("device_id", device_id);
        map.put("predicted_stop_id", predicted_stop_id);
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        map.put("velocity", velocity);
        map.put("direction", 118.0);
        map.put("weather_temperature", weather_temperature);
        map.put("weather_info", weather_info);

//        String model_path = this.getServletContext().getRealPath("model.pmml");
        String model_path = "D:/javaweb/mlmodel/model.pmml";
        Evaluator evaluator = loadModel(model_path);

        try{
            List<InputField> inputFields = evaluator.getInputFields();
            Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();
            for(InputField inputField : inputFields) {
                FieldName inputFieldName = inputField.getName();
                Object rawValue = map.get(inputFieldName.getValue());
                FieldValue inputFieldValue = inputField.prepare(rawValue);
                arguments.put(inputFieldName, inputFieldValue);
            }
            Map<FieldName, ?> results = evaluator.evaluate(arguments);
            List<TargetField> targetFields = evaluator.getTargetFields();

            FieldName targetFieldName = targetFields.get(0).getName();
            Object targetFieldValue = results.get(targetFieldName);

            Message msg = new Message();
            msg.setData(targetFieldValue);
            String json = JSONUtil.toJSON(msg);
            PrintWriter writer = response.getWriter();
            writer.print(json);
//            System.out.println(targetFieldValue);
        }catch (Exception e){
            System.out.println(e);
        }
    }

    private static Evaluator loadModel(String model_path){
        PMML pmml = new PMML();
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(model_path);
            pmml = PMMLUtil.unmarshal(inputStream);
        }catch (Exception e) {
            e.printStackTrace();
        }

        ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
        Evaluator evaluator = modelEvaluatorFactory.newModelEvaluator(pmml);

        return evaluator;

    }
}
